package handlers

import (
	"brood-agent2/utils"
	"os"
	"path/filepath"
)

func InstallKubeApiServerImpl(installer KubeApiServerInstaller) error {

	utils.Logger.Info("install kube-apiserver begin")
	err := installCheck("kube-apiserver.service")
	if err != nil {
		return err
	}

	// 下载解压
	err = extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, "kubernetes")
	if err != nil {
		return err
	}

	// 创建软连接
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kube-apiserver"), "/usr/bin/kube-apiserver")
	if err != nil {
		return err
	}
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kubectl"), "/usr/bin/kubectl")
	if err != nil {
		return err
	}

	// 下载所需配置
	utils.Logger.Info("download depend files")
	for _, download := range installer.DependFiles {
		err = utils.DownloadFile(download.Url, download.SavePath, os.FileMode(0755), false)
		if err != nil {
			return err
		}
	}

	err = utils.RegisterService("kube-apiserver.service")
	if err != nil {
		return err
	}

	utils.Logger.Info("add env for kubectl")
	err = utils.WriteStringToFile("/root/.bashrc", "\n"+installer.KubectlEnv, os.O_APPEND, os.FileMode(0755))
	if err != nil {
		return err
	}

	utils.Logger.Info("install kube-apiserver done")

	return nil
}

func InstallKubeSchedulerImpl(installer KubeSchedulerInstaller) error {
	utils.Logger.Info("install kube-scheduler begin")
	err := installCheck("kube-scheduler.service")
	if err != nil {
		return err
	}

	// 下载解压
	err = extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, "kubernetes")
	if err != nil {
		return err
	}

	// 创建软连接
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kube-scheduler"), "/usr/bin/kube-scheduler")
	if err != nil {
		return err
	}

	// 下载所需配置
	utils.Logger.Info("download depend files")
	for _, download := range installer.DependFiles {
		err = utils.DownloadFile(download.Url, download.SavePath, os.FileMode(0755), false)
		if err != nil {
			return err
		}
	}

	err = utils.RegisterService("kube-scheduler.service")
	if err != nil {
		return err
	}

	utils.Logger.Info("install kube-scheduler done")
	return nil
}

func InstallKubeControllerManagerImpl(installer KubeControllerManagerInstaller) error {
	utils.Logger.Info("install kube-controller-manager begin")
	err := installCheck("kube-controller-manager.service")
	if err != nil {
		return err
	}

	// 下载解压
	err = extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, "kubernetes")
	if err != nil {
		return err
	}

	// 创建软连接
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kube-controller-manager"), "/usr/bin/kube-controller-manager")
	if err != nil {
		return err
	}

	// 下载所需配置
	utils.Logger.Info("download depend files")
	for _, download := range installer.DependFiles {
		err = utils.DownloadFile(download.Url, download.SavePath, os.FileMode(0755), false)
		if err != nil {
			return err
		}
	}

	err = utils.RegisterService("kube-controller-manager.service")
	if err != nil {
		return err
	}

	utils.Logger.Info("install kube-controller-manager done")
	return nil
}

func InstallKubeletImpl(installer KubeletInstaller) error {
	utils.Logger.Info("install kubelet begin")
	err := installCheck("kubelet.service")
	if err != nil {
		return err
	}

	// 下载解压
	err = extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, "kubernetes")
	if err != nil {
		return err
	}

	// 创建软连接
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kubelet"), "/usr/bin/kubelet")
	if err != nil {
		return err
	}

	// 下载所需配置
	utils.Logger.Info("download depend files")
	for _, download := range installer.DependFiles {
		err = utils.DownloadFile(download.Url, download.SavePath, os.FileMode(0755), false)
		if err != nil {
			return err
		}
	}

	err = utils.RegisterService("kubelet.service")
	if err != nil {
		return err
	}

	utils.Logger.Info("install kubelet done")

	return nil
}

func InstallKubeProxyImpl(installer KubeProxyInstaller) error {
	utils.Logger.Info("install kube-proxy begin")

	err := installCheck("kube-proxy.service")
	if err != nil {
		return err
	}

	// 下载解压
	err = extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, "kubernetes")
	if err != nil {
		return err
	}

	// 创建软连接
	err = addSoftLink(filepath.Join(installer.ApplicationDir, "kubernetes", "server", "bin", "kube-proxy"), "/usr/bin/kube-proxy")
	if err != nil {
		return err
	}

	// 下载所需配置
	utils.Logger.Info("download depend files")
	for _, download := range installer.DependFiles {
		err = utils.DownloadFile(download.Url, download.SavePath, os.FileMode(0755), false)
		if err != nil {
			return err
		}
	}

	err = utils.RegisterService("kube-proxy.service")
	if err != nil {
		return err
	}

	utils.Logger.Info("install kube-proxy done")

	return nil
}

func InstallBashCompletion(installer BashCompletionInstaller) error {
	utils.Logger.Info("install bash-completion begin")

	pkgName := "bash-completion"

	// 下载解压
	err := extractPkg(installer.PkgUrl.Url, installer.ApplicationDir, installer.PkgUrl.ExtractType, pkgName)
	if err != nil {
		return err
	}
	err = utils.WriteStringToFile("/root/.bashrc", "\nsource <(kubectl completion bash)", os.O_APPEND, os.FileMode(0755))
	if err != nil {
		return err
	}

	utils.Logger.Info("install bash-completion done")

	return nil
}
